import Vue from 'vue'
import Router from 'vue-router'
const isWap = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)
const { prodUrl, testUrl } = require('../../var')

let preUrl
if (process.env.CMS_ENV) {
  preUrl = testUrl
} else {
  preUrl = prodUrl
}

const index = () => import('@/components/index/index')
const product = () => import('@/components/product/wages')
const list = () => import('@/components/list/list')
const details = () => import('@/components/details/details')
const aboutUs = () => import('@/components/aboutUs/aboutUs')

Vue.use(Router)

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/index',
      name: 'index',
      component: index
    },
    {
      path: '/product/:productType/:id',
      name: 'product',
      component: product
    },
    {
      path: '/list/:type',
      name: 'list',
      component: list,
      beforeEnter(to, from, next) {
        let { params: { type } } = to
        let url = `list.html?code=${type}`
        if (isWap) {
          next()
        } else {
          location.href = preUrl + url
        }
      }
    },
    {
      path: '/details/:type/:id',
      name: 'details',
      component: details,
      beforeEnter(to, from, next) {
        let { params: { id, type } } = to
        let url = `detail.html?from=0&id=${id}&code=${type}`
        console.log(preUrl)
        if (isWap) {
          next()
        } else {
          location.href = preUrl + url
        }
      }
    },
    {
      path: '/aboutUs/:index',
      name: 'aboutUs',
      component: aboutUs
    }
  ]
})
